/*
 * File Name��MediaPlayView.java
 * Copyright��Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description�� MediaPlayView.java
 * Modify By��res-mingyang
 * Modify Date��2015-8-4
 * Modify Type��Add
 */
package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.ciwong.mobilelib.bean.MediaPlayInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 
 * @author res-mingyang
 * @version ciwong v.1.0 2015-8-4
 * @since ciwong v.1.0
 */
public class MediaPlayView extends ImageView
{
    private Context mContext;

    private final int NORMAL_COLOR = Color.parseColor("#f9fee8");

    private Paint paint;

    private int COUNT = 15;

    private float PER_VIEW_WIDTH;

    private List<MediaPlayInfo> list;

    private float CHANGE_HEIGHT = 12;

    private float LOW_CHANGE_HEIGHT = 2;

    private Path path;

    private int mCurretnStatu;

    private final int STATU_PLAY = 1;

    private final int STATU_STOP = 0;

    public MediaPlayView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public MediaPlayView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MediaPlayView(Context context)
    {
        this(context, null);
    }

    private void init()
    {
        path = new Path();
        int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
        int screenHeight = mContext.getResources().getDisplayMetrics().heightPixels;
        PER_VIEW_WIDTH = screenWidth * 1.0f / 200;
        CHANGE_HEIGHT = screenHeight * 1.0f / 160;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Style.STROKE);
        paint.setColor(NORMAL_COLOR);
        paint.setStrokeWidth(PER_VIEW_WIDTH);
        PathEffect effects = new DashPathEffect(new float[]{16, 2, 16, 2}, 1);
        paint.setPathEffect(effects);
        paint.setAlpha(180);
        // setLayerType(LAYER_TYPE_HARDWARE, paint);
        list = new ArrayList<MediaPlayInfo>();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        if (list == null || list.size() == 0)
        {
            COUNT = (int) (width / PER_VIEW_WIDTH / 8);
            for (int i = 0; i < COUNT; i++)
            {
                MediaPlayInfo info = new MediaPlayInfo();
                info.setId(i);
                info.setWidth(PER_VIEW_WIDTH);
                info.setStartX(PER_VIEW_WIDTH * 8 * (i + 1));
                info.setStartY(height);
                info.setCurrentHeight(0);
                list.add(info);
            }
        }
        int randomHeight = 0;
        float reduceHeight = 0;
        float changeHeight = 0;
        switch (mCurretnStatu)
        {
            case STATU_PLAY:
                randomHeight = height;
                reduceHeight = 11;
                changeHeight = CHANGE_HEIGHT;
                break;
            case STATU_STOP:
                randomHeight = (int) (height * 0.1f);
                reduceHeight = 3;
                changeHeight = 2;
                break;
        }
        for (int i = 0; i < COUNT; i++)
        {
            MediaPlayInfo info = list.get(i);
            if (mCurretnStatu == STATU_STOP)
            {
                if (info.getCurrentHeight() > randomHeight)
                {
                    reduceHeight = 11*2;
                    changeHeight = CHANGE_HEIGHT * 2;
                }
                else
                {
                    reduceHeight = 3;
                    changeHeight = 2;
                }
            }

            if (Math.abs(info.getCurrentHeight() - info.getDescHeight()) < reduceHeight)
            {
                Random random = new Random();
                info.setDescHeight(random.nextInt(randomHeight));
            }
            else
            {
                if (info.getCurrentHeight() > info.getDescHeight())
                {
                    info.setCurrentHeight(info.getCurrentHeight()
                            - changeHeight);
                }
                else
                {
                    info.setCurrentHeight(info.getCurrentHeight()
                            + changeHeight);
                }
            }
            LinearGradient lg = new LinearGradient(info.getStartX(), height,
                    info.getStartX(), height - info.getCurrentHeight(),
                    NORMAL_COLOR, Color.parseColor("#AFE368"),
                    Shader.TileMode.CLAMP);
            paint.setShader(lg);
            path.moveTo(info.getStartX(), height);
            path.lineTo(info.getStartX(), height - info.getCurrentHeight());
            canvas.drawPath(path, paint);
            path.reset();
        }
        System.gc();
        invalidate();
    }

    public void stop()
    {
        this.mCurretnStatu = STATU_STOP;
    }

    public void play()
    {
        this.mCurretnStatu = STATU_PLAY;
    }

}
